package com.acme.domain.account;


import com.acme.exceptions.NoEnoughFundsException;

public abstract class AbstractAccount implements Account {

    private double balance;

    @Override
    public void deposit(final double amount) throws IllegalArgumentException {
        if (amount < 0) {
            throw new IllegalArgumentException();
        }
        this.balance += amount;
    }

    @Override
    public void withdraw(final double amount) throws NoEnoughFundsException, IllegalArgumentException {
        if (amount < 0) {
            throw new IllegalArgumentException();
        }
        if (this.balance - amount >= 0) {
            this.balance -= amount;
        } else {
            throw new NoEnoughFundsException("from AbstractAccount");
        }
    }

    @Override
    public int decimalValue() {
        return Math.round((float)this.balance);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AbstractAccount that = (AbstractAccount) o;
        if (Double.compare(that.balance, balance) != 0) return false;
        return true;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(balance);
        return (int) (temp ^ (temp >>> 32));
    }

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }

    public AbstractAccount(final double amount) {
        this.balance = amount;
    }

    public double getBalance() {
        return balance;
    }
}
